<?php
/**
 * 公司
 */
require_once("../global.inc.php");

class SysCompanyClass extends SystemFrameWork{
	/**
	 * 公司对象
	 *
	 * @var obj
	 */
	var $obj_company;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	function main(){
		/**
		 * 客户公司
		 */
		if (!is_object($this->obj_company)){
			require_once ("company.class.php");
			$this->obj_company = new CompanyClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
            case 'list':
                $this->_list();
                break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case "del":
				$this->_del();
				break;
			case "view":
				$this->_view();
				break;
			case "ajax_get_company":
				$this->_ajax_get_company();
				break;
			case 'withlist':
                $this->_withlist();
                break;
            case 'dowith':
                $this->_dowith();
                break;
            case 'unwith':
                $this->_unwith();
                break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
		$type = intval($this->_input['t']);
		//搜索条件
		$condition = " where type='".$type."' and status='1' order by id desc ";

		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$company_array = $this->obj_company->listCompany($condition,$this->obj_page);
		
		//对时间进行格式化
		if (is_array($company_array)){
			for ($i=0;$i<count($company_array);$i++){
				$company_array[$i]['create_time'] = @date("Y-m-d",$company_array[$i]['create_time']);
			}
		}

		$page_list = $this->obj_page->show(2);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('company_array',$company_array);
		$this->output('page_list',$page_list);
		$this->output('type',$type);
		$this->showpage('company.list');
	}
	/**
	 * 信息添加
	 */
	function _add(){
		$type = intval($this->_input['t']);
		if ($type != 1 && $type != 2 && $type != 3) {
			$type = 1;
		}
		$this->output('type',$type);
		$this->showpage('company.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		$type = intval($this->_input['t']);
		$typename = '';
		if ($type == 1) {
			$typename = '客户';
		}elseif ($type == 2){
			$typename = '媒介';
		}elseif ($type == 3){
			$typename = '供应商';
		}
		$value_array = array();
		$value_array['type'] = $type;  //类型（1客户 2媒体 3供应商）
		$value_array['realname'] = trim($this->_input["realname"]);  //名称
		$value_array['industry'] = trim($this->_input["industry"]);  //行业
		$value_array['website'] = trim($this->_input["website"]);  //网址
		$value_array['kind'] = $this->_input["kind"] ? $this->_input["kind"] : 0;  //性质
		$value_array['address'] = trim($this->_input["address"]);  //地址
		$value_array['phone'] = trim($this->_input["phone"]);  //电话
		$value_array['post'] = trim($this->_input["post"]);  //邮编
		$value_array['intro'] = trim($this->_input["intro"]);  //介绍
		$value_array['create_id'] = $_SESSION['s_admin']['id'];  //创建者ID
		$value_array['create_time'] = time();  //创建时间
		$value_array['status'] = 1;  //状态
		$result = $this->obj_company->addCompany($value_array);
			
		if ($result != true){
			$this->redirectPath('error','','添加'.$typename.'失败！',1);
		}else {
			//记录操作日志
			SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->添加'.$typename);
			$this->redirectPath('error','./company.manage.php?action=list&t='.$type,'添加'.$typename.'成功！',1);
		}
		
	}
	
	/**
	 * 修改信息
	 */
	function _modi(){
		$id = $this->_input["id"];
		$type = $this->_input["t"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$company_array = $this->obj_company->getCompany($id);
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('company_array',$company_array);
			$this->output('type',$type);
			$this->showpage('company.modi');
		}
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsIdIsEmpty']),
//			array("input"=>$this->_input["txttitle"], "require"=>"true", "message"=>$this->_lang['errSysNNewsNameIsEmpty']),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong']),		
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$type = $this->_input["t"];
			$typename = '';
			if ($type == 1) {
				$typename = '客户';
			}elseif ($type == 2){
				$typename = '媒介';
			}elseif ($type == 3){
				$typename = '供应商';
			}
		
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['realname'] = trim($this->_input["realname"]);  //名称
			$value_array['industry'] = trim($this->_input["industry"]);  //行业
			$value_array['website'] = trim($this->_input["website"]);  //网址
			$value_array['kind'] = $this->_input["kind"] ? $this->_input["kind"] : 0;  //性质
			$value_array['address'] = trim($this->_input["address"]);  //地址
			$value_array['phone'] = trim($this->_input["phone"]);  //电话
			$value_array['post'] = trim($this->_input["post"]);  //邮编
			$value_array['intro'] = trim($this->_input["intro"]);  //介绍
			$result = $this->obj_company->updateCompany($value_array);
			
			if ($result != true){
				$this->redirectPath('error','','更新'.$typename.'失败！',1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->更新'.$typename);
				$this->redirectPath('error','./company.manage.php?action=list&t='.$type,'更新'.$typename.'成功！',1);
			}
//		}
	}
	
	/**
	 * 修改信息
	 */
	function _view(){
	    $id = $this->_input["id"];
		$type = $this->_input["t"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$company_array = $this->obj_company->getCompany($id);
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('company_array',$company_array);
			$this->output('type',$type);
			$this->showpage('company.view');
		}
	}
	/**
	 * 删除信息
	 */
	function _del(){
		$type = $this->_input["t"];
		$typename = '';
		if ($type == 1) {
			$typename = '客户';
		}elseif ($type == 2){
			$typename = '媒介';
		}elseif ($type == 3){
			$typename = '供应商';
		}
		if (is_array($this->_input['id'])){
			foreach ($this->_input['id'] as $v) {
				$this->obj_company->delCompany($v);
			}
			//记录操作日志
			SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->删除'.$typename);
			$this->redirectPath('error','','删除'.$typename.'成功！',1);
		}else {
			$this->redirectPath('error', '', '删除'.$typename.'失败！', 1);
		}
	}
	
	/**
	 * 关联列表
	 */
	function _withlist(){
		$obj_id = $this->_input["id"];
		$type = $this->_input["t"];
		//获取当前对象
		$obj_array = $this->obj_company->getCompany($obj_id);
		
		if ($type == 2) {
			$where = " where vendor_id='".$obj_id."' "; //媒体列表
		}else {
			$where = " where media_id='".$obj_id."' "; //供应商列表
		}
		//获取所有关联对象
		$with_array = $this->obj_company->listCompanywith($where);
		//print_r($with_array);
		//搜索条件
		$condition = " where type='".$type."' and status='1' order by id desc ";
		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$company_array = $this->obj_company->listCompany($condition,$this->obj_page);
		
		//对时间进行格式化
		if (is_array($company_array)){
			for ($i=0;$i<count($company_array);$i++){
				$company_array[$i]['create_time'] = @date("Y-m-d",$company_array[$i]['create_time']);
				//匹配已关联对象
				$is_with = 0;
				if ($with_array) {
					foreach ($with_array as $val){
						if($type == 2 && $val['media_id'] == $company_array[$i]['id']){
							$is_with = $val['id'];
							break;
						}elseif ($type == 3 && $val['vendor_id'] == $company_array[$i]['id']){
							$is_with = $val['id'];
							break;
						}
					}
				}
				$company_array[$i]['is_with'] = $is_with;
			}
		}
		//print_r($company_array);
		$page_list = $this->obj_page->show(2);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('company_array',$company_array);
		$this->output('page_list',$page_list);
		$this->output('obj_id',$obj_id);
		$this->output('obj_array',$obj_array);
		$this->output('type',$type);
		$this->showpage('company.withlist');
	}

	/**
	 * 添加关系
	 */
	function _dowith(){
		$obj_id = $this->_input["obj_id"];
		$ids = $this->_input["id"];
		$type = $this->_input["t"];
		
		if (is_array($ids)){
			foreach ($ids as $v) {
				$value_array = array();
				$value_array['media_id'] = $obj_id;
				$value_array['vendor_id'] = $v;
				$value_array['user_id'] = $_SESSION['s_admin']['id'];
				$value_array['create_time'] = time();
				$this->obj_company->addCompanywith($value_array);
			}
			
			//记录操作日志
			if ($type == 2) {
				SystemPowerClass::addSysLog('媒体->供应商管理->添加媒体关联');
				$this->redirectPath('error','','添加成功！',1);
			}elseif ($type == 3){
				SystemPowerClass::addSysLog('媒体->媒体管理->添加供应商关联');
				$this->redirectPath('error','','添加成功！',1);
			}
			
		}else {
			$value_array = array();
			$value_array['media_id'] = $obj_id;
			$value_array['vendor_id'] = $ids;
			$value_array['user_id'] = $_SESSION['s_admin']['id'];
			$value_array['create_time'] = time();
			$this->obj_company->addCompanywith($value_array);
			
			//记录操作日志
			if ($type == 2) {
				SystemPowerClass::addSysLog('媒体->供应商管理->添加媒体关联');
				$this->redirectPath('error','','添加成功！',1);
			}elseif ($type == 3){
				SystemPowerClass::addSysLog('媒体->媒体管理->添加供应商关联');
				$this->redirectPath('error','','添加成功！',1);
			}
		}
	}
	
	/**
	 * 解除关系
	 *
	 */
	function _unwith(){
		$type = $this->_input["t"];
		$id = $this->_input["id"];
		$this->obj_company->unCompanywith($id);
		//记录操作日志
		if ($type == 2) {
			SystemPowerClass::addSysLog('媒体->供应商管理->删除媒体关联');
			$this->redirectPath('error','','删除成功！',1);
		}elseif ($type == 3){
			SystemPowerClass::addSysLog('媒体->媒体管理->删除供应商关联');
			$this->redirectPath('error','','删除成功！',1);
		}
	}
	/**
	 * 返回页面的形式为 option格式
	 */
	function _ajax_get_company(){
		if (!is_object($obj_ajax_page)) {
			require_once("ajaxcommonpage.class.php");
			$obj_ajax_page = new ajaxCommonPage();
		}
		$obj_ajax_page->pagebarnum(10);
		$company_name = Common::unescape($this->_input['txtCompanyName'],$this->_configinfo['websit']['ncharset']);
		$where = 'where type=1';
		if ($company_name != '') {
			$where = " and realname like '%".$company_name."%' ";
		}
		$company_array = $this->obj_company->listCompany($where, $obj_ajax_page);
		$page_list = $obj_ajax_page->show(2);

		/**
		 * 页面输出
		 */
		$this->output('page_list',$page_list);
		$this->output('company_array',$company_array);
		$this->showpage("sys_module_company.result");
	}

}
$company = new SysCompanyClass();
$company->main();
unset($company);
?>